<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>摆动序列</title>
</head>
<body>
  <script>
    function fn(nums) {
      if (nums.length < 2) return nums.length;
      let down = 1, up = 1;
      for (let i = 1; i < nums.length; i++) {
        if (nums[i] > nums[i - 1]) {
          up = down + 1;
        } else if (nums[i] < nums[i - 1]) {
          down = up + 1;
        }
      }
      return Math.max(down, up);
    }
    console.log(fn([1, 7, 4, 9, 2, 5]))
  </script>
</body>
</html>